/**
 * @version: V1.0
 * @Title: DeepSerAndDeSer.java
 * @Description:
 * @author wangshipeng
 * @date 2022-01-20 23:30
 * @Copyright: 2022-01-20
 */

package com.peng.tree.serialization;

import com.peng.tree.Node;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author wangshipeng
 * @ClassName: DeepSerAndDeSer
 * @Description: 深度遍历版序列化/反序列化
 * @date 2022-01-20 23:30
 */
public class DeepSerAndDeSer {

    public static void preSerialize(Node head, Queue<String> collect) {
        if (head == null) {
            collect.add(null);
            return;
        }
        collect.add(String.valueOf(head.value));

        preSerialize(head.left, collect);

        preSerialize(head.right, collect);


    }

    public static Node preDeserialize(Queue<String> nodes) {

        String val = nodes.poll();
        if (val == null) {
            return null;
        }

        Node head = new Node(Integer.parseInt(val));

        head.left = preDeserialize(nodes);
        head.right = preDeserialize(nodes);


        return head;

    }

    public static void main(String[] args) {
        Node node = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);
        Node node7 = new Node(7);
        Node node8 = new Node(8);
        Node node9 = new Node(9);

        node.left = node2;
        node.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        node4.left = node8;
        node4.right = node9;
        /*
                   1
             2              3
           4   5         6     7
         8   9

         */
        Queue<String> collect = new LinkedList<>();
        preSerialize(node, collect);

        System.out.println(collect);

        Node node01 = preDeserialize(collect);

        System.out.println(node01);
    }
}
